package cn.lg.soar.redis.aspect.clusterlock;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * el表达式方式配置
 * @author luguoxiang
 * @date 2022/6/6
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
class ElConfig implements IConfig{

    private final String prefix; // 锁前缀
    private final String[] paramNames; // 参数列表参数名数组
    private final Expression expression;

    public ElConfig(String prefix, String[] paramNames, Expression expression) {
        this.prefix = prefix;
        this.paramNames = paramNames;
        this.expression = expression;
    }

    @Override
    public String lockName(ProceedingJoinPoint point) {
        EvaluationContext context = new StandardEvaluationContext();
        Object[] arguments = point.getArgs();
        for(int i=0; i < arguments.length; i++){
            context.setVariable(this.paramNames[i], arguments[i]);
        }
        return this.prefix + this.expression.getValue(context, String.class);
    }

}
